package de.lulusoft.anothertorrent.gui.views.Providers;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.swt.graphics.Image;

import de.lulusoft.anothertorrent.core.rtorrent.Torrent;
import de.lulusoft.anothertorrent.gui.AnotherTorrentGui;
import de.lulusoft.anothertorrent.gui.util.Util;

public class TorrentListViewLabelProvider implements ITableLabelProvider {
	
	private static Image stopImage = null;
	private static Image startImage = null;

	@Override
	public void removeListener(ILabelProviderListener listener) {
	}

	@Override
	public boolean isLabelProperty(Object element, String property) {
		return false;
	}

	@Override
	public void dispose() {
	}

	@Override
	public void addListener(ILabelProviderListener listener) {
	}

	@Override
	public String getColumnText(Object element, int columnIndex) {
		if (element instanceof Torrent)
		{
			Torrent t = (Torrent)element;
			switch (columnIndex)
			{
			case 0: return t.getName();
			case 1: return null; //t.getState().toString();
			case 2: return t.getDonePercent().toString() + " %";
			case 3: return Util.formatBytes(t.getRemainingBytes());
			case 4: return Util.formatBytes(t.getSize_bytes());
			case 5: return Util.formatBytes(t.getDown_rate());
			case 6: return Util.formatBytes(t.getUp_rate());
			case 7: return Util.formatBytes(t.getUp_total());
			case 8: return t.getRatioFormated();
			case 9: return t.getPeers_connected().toString() + " (" + 
				t.getPeers_accounted() + "/" + t.getPeers_complete() + 
				"/" + t.getPeers_not_connected() + ")";
			case 10: return t.getPriority_str().toString();
			
			default: return "Not yet implemented";
			}
		}
		return "";
	}

	@Override
	public Image getColumnImage(Object element, int columnIndex) {
		if (element instanceof Torrent)
		{
			Torrent t = (Torrent)element;
			switch (columnIndex)
			{
			case 1: 
			{
				if (t.getState() == 0L)
				{
					if (stopImage == null)
						stopImage = AnotherTorrentGui.createImage("stop.png");
					return stopImage;
				}
				else if (t.getState() == 1L)
				{
					if (startImage == null)
						startImage = AnotherTorrentGui.createImage("control_play_blue.png");
					return startImage;
				}
			}break;
			default: return null;
			}
		}
		return null;
	}

}
